<?php
/**
 * WpStopwatch.php
 * 
 * This file contains the WpStopwatch class, a simple stopwatch
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage util
 */

/**
 * A stopwatch class
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage util
 */
class WpStopwatch
{
	public $_arrRuns = array();
	
	/** 
	 * Starts the stopwatch
	 * @param string $issue An identifier to assign the stop time later on. This let you run several stopwatches 
	 * in parallel
	 */
	public function start($issue)
	{
		if ($issue === '') {
			$issue = 'wpstopwatch_placeholder'; //Dont wanna have a empty string index in array
		}
		
		$this->_arrRuns[$issue] = array(
			'start' => microtime(true),
			'end' => microtime(true)
		);
	}
	
	/** 
	 * Stops the stopwatch and returns the time since start
	 * @param string $issue The identifier provided when calling start() 
   * format, just provide another factor
	 * @return int The time since activating start function or 0, if start action was not activated
	 */
	public function stop($issue)
	{
		if ($issue === '') {
			$issue = 'wpstopwatch_placeholder'; //Dont wanna have a empty string index in array
		}
		if (!array_key_exists($issue, $this->_arrRuns)) {
			return 0;
		}
		
		$this->_arrRuns[$issue]['end'] = microtime(true);
		
		return ($this->_arrRuns[$issue]['end'] - $this->_arrRuns[$issue]['start']);
		
	}
}

/* End of file WpStopwatch.php */
/* Location: ./wildpack/util/WpStopwatch.php */ 
